package org.gtugs.bremen.multilabyrinth.scene.elements;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;


public class WallElement extends Element{
	
	public enum Direction{
		HORIZONTAL,
		VERTICAL;
		
		public static Direction findDirection(final String param){
			final Direction result;
			if(param.equals("HORIZONTAL")){
				result = HORIZONTAL;
			}else if(param.equals("VERTICAL")){
				result = VERTICAL;
			}else{
				result = HORIZONTAL;
			}
			return result;
		}
	}
	
	public enum WallSize{
		SMALL,
		NORMAL;
		
		public static WallSize findWallSize(final String param){
			final WallSize result;
			if(param.equals("SMALL")){
				result = SMALL;
			}else if(param.equals("NORMAL")){
				result = NORMAL;
			}else{
				result = NORMAL;
			}
			return result;
		}
	}

	private final float[] positions;
	
	private Direction direction;
	
	private WallSize size;
	
	public WallElement(final float pX1, final float pY1, final Direction direction, final WallSize size){
		this.positions = new float[2];
		this.positions[0] = pX1;
		this.positions[1] = pY1;
		this.direction=direction;
		this.size=size;
	}

	public WallElement(JSONObject json) {
		this.positions = new float[2];
		try {
			this.positions[0] = Float.parseFloat(json.getString("positions0"));
			this.positions[1] = Float.parseFloat(json.getString("positions1"));
			this.direction=Direction.findDirection(json.getString("direction"));
			this.size=WallSize.findWallSize(json.getString("size"));
		} catch (JSONException e) {
			this.direction=null;
			this.size=null;
			Log.e(this.getClass().getSimpleName(), "JSONException occured: " + e.getMessage());
		}
	}

	@Override
	public ElementKind getElementKind() {
		return ElementKind.WALL;
	}

	@Override
	public float[] getPositions() {
		return this.positions;
	}
	
	/**
	 * @return the direction
	 */
	public Direction getDirection() {
		return direction;
	}

	/**
	 * @return the size
	 */
	public WallSize getSize() {
		return size;
	}

	@Override
	public JSONObject toJSON() {
		final JSONObject json = new JSONObject();
		try{
			json.put("positions0", String.valueOf(this.positions[0]));
			json.put("positions1", String.valueOf(this.positions[1]));
			json.put("direction", this.direction.toString());
			json.put("size", this.size.toString());
			json.put("kind", this.getElementKind().toString());
		}catch(JSONException e){
			Log.e("BallElement", "JSONException occured: " + e.getMessage());
		}
		return json;
	}
}